﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using POSGui.Clases.Entities;
using POSGui.Presentacion;

namespace POSGui.Classes.BusinessLogic
{
    public class CartManager
    {
        private ERPSIA.Document _invoice;
        public Sale _currentSale = new Sale();
        public List<DatosProducto> _carritoDeCompras = new List<DatosProducto>();
        public List<Product> _produc = new List<Product>();

        #region Singleton
        private static CartManager _instance;

        private CartManager(WebServiceAgent agent)
        {
            _invoice = agent.PreviewInvoiceDocument();
        }

        public static CartManager GetInstance(WebServiceAgent agent)
        {
            if (_instance == null)
            {
                _instance = new CartManager(agent);
            }
            return _instance;
        }
        #endregion

        public void UpdateItem(string code, int quantity)
        {
            foreach (ERPSIA.DocDetail item in _invoice.detalleDoc)
            {
                if (item.Item.CodArticulo == code)
                {
                    item.Quantity += quantity;
                    return;
                }
            }
        }

        public void AddItem(WebServiceAgent agent, ERPSIA.Item item, int quantity)
        {
            List<ERPSIA.DocDetail> items;
            ERPSIA.DocDetail newItem;

            items = _invoice.detalleDoc.ToList<ERPSIA.DocDetail>();
            newItem = new ERPSIA.DocDetail();

            newItem.IdDocumentoContable = Constants.INVALID_ID;
            newItem.Item = item;
            newItem.Quantity = quantity;
            newItem.StoreRoom = InventoryManager.GetStore(InventoryManager.GetInstance().GetCurrentStoreRoom(agent), agent.GetStoreRooms());
            newItem.Tax = agent.GetDefaultTax();
            newItem.UnitCost = InventoryManager.GetUnitCost(newItem.Item, newItem.StoreRoom);

            items.Add(newItem);
            _invoice.detalleDoc = items.ToArray<ERPSIA.DocDetail>();
        }

        public void RemoveItem(int index)
        {
            List<ERPSIA.DocDetail> items;

            items = _invoice.detalleDoc.ToList<ERPSIA.DocDetail>();
            items.RemoveAt(index);
            _invoice.detalleDoc = items.ToArray<ERPSIA.DocDetail>();
             
        }

        public ERPSIA.Document CheckCart()
        {
            return this._invoice;
        }

        public bool CheckOut(WebServiceAgent agent, int moneda)
        {
            _invoice.IdMoneda = moneda;
            _invoice.subTotal = 0;
            bool payed;

            foreach (ERPSIA.DocDetail item in _invoice.detalleDoc)
            {
                _invoice.subTotal += item.Quantity * item.UnitCost;
            }

            _invoice.impuestos = agent.GetDefaultTax().Porcentaje * _invoice.subTotal;
            _invoice.total = _invoice.subTotal + _invoice.impuestos;
            payed = agent.SaveClienInvoiceDocument(_invoice);
            _invoice = agent.PreviewInvoiceDocument();
            return payed;
        }
    }
}